package com.ming.exception;

import com.ming.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 * @author MING
 * @date 2018/8/9 10:41
 */
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {

    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object object, Exception exception) {
        // 如果不是ajax，返回一个自定义错误页面
        if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request
                .getHeader("X-Requested-With") != null && request.getHeader(
                "X-Requested-With").indexOf("XMLHttpRequest") > -1))) {
            if (exception instanceof PermissionException || exception instanceof ParamException) {
                JsonData result = JsonData.fail(exception.getMessage());
                log.error("My page exception, url:" + request.getRequestURL().toString(), exception);
                return new ModelAndView("/404", result.toMap());
            }else if (exception instanceof UnauthorizedException){
//                由于使用的是shiro注解配置权限，在访问没有权限的路径时，会抛出这个异常，
//                将这个异常进行处理，转到没有权限页面，避免跳转到500页面，当然如果是在
//                shiro.xml页面进行配置的，就不要做这个异常处理，unauthorizedUrl会处理
                JsonData result = JsonData.fail("没有权限！");
                log.error("My Function exception, url:" + request.getRequestURL().toString(), exception);
                return new ModelAndView("/nofunc", result.toMap());
            }else {
                JsonData result = JsonData.fail("系统异常！");
                log.error("unknown page exception, url:" + request.getRequestURL().toString(), exception);
                return new ModelAndView("/500", result.toMap());
            }
        } else {
            // 如果是ajax请求，JSON格式返回
            JsonData result;
            if (exception instanceof PermissionException || exception instanceof ParamException) {
                result = JsonData.fail(exception.getMessage());
                log.error("My json exception, url:" + request.getRequestURL().toString(), exception);
            }else if (exception instanceof UnauthorizedException){
                result = JsonData.fail("没有权限！");
                log.error("My Function exception, url:" + request.getRequestURL().toString(), exception);
            } else {
                result = JsonData.fail("系统异常！");
                log.error("unknown json exception, url:" + request.getRequestURL().toString(), exception);
            }
            ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView() ,result.toMap());
            return modelAndView;
        }
    }
}
